package thread;

import java.util.concurrent.*;
import java.util.function.Supplier;

public class ThreadTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开启了");
        //1.继承thread
//        new Thread(new Thread1()).start();
        //2.实现runnable接口
//        new Thread(new Thread2()).start();
        //3.实现callable接口+FutureTask
        FutureTask<Integer> futureTask = new FutureTask<>(new Thread3());
//        new Thread(futureTask).start();
//        //get方法会阻塞主线程
//        Integer integer = futureTask.get();
//        System.out.println("在主线程中获取到结果"+integer);
        //4.使用线程池
        //4.1使用executors工具类
//        ExecutorService threadPool = Executors.newFixedThreadPool(10);
//        threadPool.execute(new Thread(futureTask));
        //4.2使用原始线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5, 10, 10, TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(50),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
//        threadPoolExecutor.execute(futureTask);

        //使用CompletableFuture编排异步任务
//        CompletableFuture.runAsync(futureTask,threadPoolExecutor);
        CompletableFuture<Integer> f1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("开启了线程1");
            int x = 8 / 2;
            return x;
        }, threadPoolExecutor);

        CompletableFuture<Integer> f2 = CompletableFuture.supplyAsync(() -> {

            int x = 8 / 2;
            try {
                Thread.sleep(3000);
                System.out.println("开启了线程2");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return x;
        }, threadPoolExecutor);

//        f1.thenAcceptBothAsync(f2,(r1,r2)->{
//            System.out.println("线程3开启了");
//        },threadPoolExecutor);
//        CompletableFuture<Integer> future = f1.thenCombineAsync(f2, (r1, r2) -> {
//            System.out.println("任务3开启了");
//            return r1 + r2;
//        }, threadPoolExecutor);
        CompletableFuture<Void> allOf = CompletableFuture.allOf(f1, f2);
        allOf.get();//等待所有任务完成（阻塞式等待）
        System.out.println("主线程结束了"+f1.get()+f2.get());
    }

    public static class Thread1 extends Thread {
        @Override
        public void run() {
            System.out.println("开启了线程" + Thread.currentThread().getId());
            int x = 8 / 2;
            System.out.println("计算出结果" + x);
        }
    }

    public static class Thread2 implements Runnable {

        @Override
        public void run() {
            System.out.println("开启了线程" + Thread.currentThread().getId());
            int x = 8 / 2;
            System.out.println("计算出结果" + x);
        }
    }

    public static class Thread3 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("开启了线程" + Thread.currentThread().getId());
            int x = 8 / 2;
            System.out.println("计算出结果" + x);
            return x;
        }
    }
}
